package com.xctech.yace.util;

import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.jasypt.util.text.BasicTextEncryptor;

public class JasyptUtils {
    public JasyptUtils() {
    }

    public static String encryptText(String password, String message) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(password.trim());
        String ciphertext = "ENC(" + basicTextEncryptor.encrypt(message) + ")";
        return ciphertext;
    }

    public static String decryptText(String password, String ciphertext) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword(password.trim());
        if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)) {
            return PropertyValueEncryptionUtils.decrypt(ciphertext, basicTextEncryptor);
        } else {
            throw new RuntimeException("密文解密失败,密文是非加密值");
        }
    }
    public static void main(String[] args) {
        BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();
        basicTextEncryptor.setPassword("trade");

        String real2023 = basicTextEncryptor.encrypt("Real2023");
        System.out.println(real2023);

        String decrypt = basicTextEncryptor.decrypt(real2023);
        System.out.println(decrypt);

        String ciphertext = "ENC("+real2023+")";
        if(PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)){
            System.out.println(PropertyValueEncryptionUtils.decrypt(ciphertext,basicTextEncryptor));
        }else{
            System.out.println("");
        }
    }
}
